package com.lzh.runke.core.entity.spec;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzh.runke.core.web_base.helper.BeanHelper;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @Description:
 * @Author: LiuZiHao
 * @Date: 2022/5/25 16:56
 */
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@Slf4j
public class BaseSpec implements Serializable {

    /**
     * 主键
     */
    private String id;

    /**
     * 页码
     */
    protected Integer pageNo;

    /**
     * 每页行数
     */
    protected Integer pageSize;

    @JsonAnySetter
    public void handleUnknown(String key, Object value) {
        log.warn("Unknown Property:{{}: {}}", key, value);
    }

    @Override
    public String toString() {
        ObjectMapper om = BeanHelper.getBeanIfAbsent(ObjectMapper.class);
        if (null == om) {
            return super.toString();
        }
        try {
            return om.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return super.toString();
    }
}
